#include "expweek.h"
#include "ui_expweek.h"
#include<QDesktopWidget>
#include<QDate>
#include<QDebug>

ExpWeek::ExpWeek(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::ExpWeek)
{
    ui->setupUi(this);
    QRect q=QApplication::desktop()->availableGeometry(this);
    ui->scrollArea->setGeometry(0,0,(q.width()),(q.height()));
    QAction* backAction = new QAction( tr("Back"), this );
    backAction->setSoftKeyRole( QAction::NegativeSoftKey );
    connect(backAction, SIGNAL(triggered()), this, SLOT(back()));
    addAction( backAction );
    int fontSize=10;//(q.width()/30.0);
    ui->label->setFont(QFont( "lucida", fontSize, QFont::Normal, FALSE ));
    ui->label_year->setFont(QFont( "lucida", (fontSize-2), QFont::Normal, FALSE ));
    ui->label_month->setFont(QFont( "lucida", (fontSize-2), QFont::Normal, FALSE ));
    ui->label_week->setFont(QFont( "lucida", (fontSize-2), QFont::Normal, FALSE ));
    ui->comboBox_year->setFont(QFont( "lucida", (fontSize-2), QFont::Normal, FALSE ));
    ui->comboBox_month->setFont(QFont( "lucida", (fontSize-2), QFont::Normal, FALSE ));
    ui->comboBox_week->setFont(QFont( "lucida", (fontSize-2), QFont::Normal, FALSE ));
    ui->view->setFont(QFont( "lucida", (fontSize-2), QFont::Normal, FALSE ));

    QStringList list;
    list<<"January"<<"February"<<"March"<<"April"<<"May"<<"June"<<"July"<<"August"<<"September"<<"October"<<"November"<<"December";
    for(int i=0;i<list.size();i++)
        ui->comboBox_month->addItem(list.at(i),Qt::UserRole);

    QDate d=QDate::currentDate();//Set current date
    for(int y=2010;y<=d.year();y++)//2010 till current year
        ui->comboBox_year->addItem(QString("%1").arg(y),Qt::UserRole);

    int find=ui->comboBox_year->findText(QString("%1").arg(d.year()));
    ui->comboBox_year->setCurrentIndex(find);
    find=ui->comboBox_month->findText(QString("%1").arg(list[d.month()-1]));
    ui->comboBox_month->setCurrentIndex(find);
}

ExpWeek::~ExpWeek()
{
    delete ui;
}
void ExpWeek::back()
{
    (new Reports)->show();
}
void ExpWeek::showWeeks(int month,int year)
{//This function is called whenver month or year combo box index is changed
    //This function will calculate number of weeks of that month and add them to the week combo box
    int i,j=1;
    for(i=0;i<7;i++)
    {
        ui->comboBox_week->removeItem(0);
    }


    QDate dayOne;
    dayOne.setDate(year,month,1);//First day of year
    int list[7]={0,0,0,0,0,0,0};
    QDate dt=dayOne;
    i=1;
    while(i<=dayOne.daysInMonth())
    {
        dt.setDate(dayOne.year(),dayOne.month(),i);

        if(dt.dayOfWeek()==7)
        {
            list[j]=i;

            j++;

        }
        i++;
    }
    int weeks=0;
    if(list[j-1]==dayOne.daysInMonth())
    {
        weeks=j-1;
    }
    else
        weeks=j;
    for(i=1;i<=weeks;i++)
        ui->comboBox_week->addItem(QString("%1").arg(i),Qt::UserRole);


}
void ExpWeek::on_comboBox_month_currentIndexChanged(int index)
{
    //qDebug()<<index;
    showWeeks((index+1),ui->comboBox_year->currentText().toInt());
}
void ExpWeek::on_comboBox_year_currentIndexChanged(int index)
{
    showWeeks((ui->comboBox_month->currentIndex()+1),ui->comboBox_year->itemText(index).toInt());
}
void ExpWeek::on_view_clicked()
{
    QDate dt;
    dt.setDate((ui->comboBox_year->currentText().toInt()),(ui->comboBox_month->currentIndex()+1),1);
    (new GraphWeekly(dt,ui->comboBox_week->currentText().toInt()))->show();
}

